package cn.zk.zkClient;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;

import java.util.List;

/**
 * ClassName: ZkClientFirst
 * Desc: 开源的 ZkClient 客户端
 * https://github.com/sgroschupf/zkclient
 * Date： 2016/12/5
 * Created：shaom
 */
public class ZkClientFirst {

    public static final String connectString = "master:2181,slave01:2181,slave02:2181";

    void createClient() {
        ZkClient client = new ZkClient(connectString, 5000);
        System.out.println("客户端创建成功~");
    }

    /**
     * 创建节点，支持递归
     */
    void createNode() {
        ZkClient client = new ZkClient(connectString, 5000);

        // 创建节点
        client.create("/ZkClient", "ZkClient".getBytes(), CreateMode.PERSISTENT);

        // 创建临时节点
        client.createEphemeral("/ZkClient1");
        client.createEphemeralSequential("/ZkClient2", "/ZkClient2".getBytes());

        // 创建持久化节点
        client.createPersistent("/ZkClient3");
        client.createPersistentSequential("/ZkClient4", "/ZkClient1".getBytes());

        // 递归创建节点，如果父节点没有，则创建
        client.createPersistent("/ZkClient5/child", true);
    }

    /**
     * 删除，支持非叶子节点的删除
     */
    void delete() {

        ZkClient client = new ZkClient(connectString, 5000);

        // 删除节点
        client.delete("/ZkClient");
        client.delete("/ZkClient2");
        client.delete("/ZkClient20000000011");
        client.delete("/ZkClient3");
        client.delete("/ZkClient4");
        client.delete("/ZkClient40000000013");

        // 删除非叶子节点
        client.deleteRecursive("/ZkClient5");

    }

    /**
     * 获取子节点
     */
    void getChild() {
        ZkClient client = new ZkClient(connectString, 5000);

        // 递归创建节点，如果父节点没有，则创建
        client.createPersistent("/ZkClient/child/grand", true);

        // 没有监听
        List<String> children = client.getChildren("/ZkClient");
        System.out.println(children);//不会递归展示

        // 注册监听,新增子节点、减少子节点、新增删除该节点
        // 注册一次，一直有效
        client.subscribeChildChanges("/ZkClient", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChildren) throws Exception {
                System.out.println(parentPath + ": " + currentChildren);
            }
        });

        // 新增子节点
        client.createPersistent("/ZkClient/child2", true);

        // 减少子节点
        client.deleteRecursive("/ZkClient/child");

        // 删除该节点
        client.deleteRecursive("/ZkClient");

    }

    /**
     * 获取值
     */
    void getData() {
        ZkClient client = new ZkClient(connectString, 5000);

        // 注册监听
        client.subscribeDataChanges("/ZkClient", new IZkDataListener() {

            // 节点内容变更
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("#handleDataChange " + dataPath + " : " + data);
            }

            // 节点删除
            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("#handleDataDeleted " + dataPath);
            }
        });

        client.createPersistent("/ZkClient", "/ZkValue");

        System.out.println((String) client.readData("/ZkClient"));

        client.writeData("/ZkClient", "/ZkValue2");
        System.out.println((String) client.readData("/ZkClient"));

        client.writeData("/ZkClient", "/ZkValue3");
        System.out.println((String) client.readData("/ZkClient"));

        client.delete("/ZkClient");


    }

    void exists() {
        ZkClient client = new ZkClient(connectString, 5000);
        client.createPersistent("/ZkClient", "/ZkValue");
        System.out.println(client.exists("/ZkClient"));

    }


}
